const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const copyWebpackPlugin = require("copy-webpack-plugin");
const path = require("path");

const mode = process.env.NODE_ENV || "development";
const prod = mode === "production";

// 项目的输出目录
const productName = "6";

module.exports = {
  entry: {
    "build/bundle": ["./src/main.js"],
  },
  resolve: {
    alias: {
      svelte: path.dirname(require.resolve("svelte/package.json")),
    },
    extensions: [".mjs", ".js", ".svelte"],
    mainFields: ["svelte", "browser", "module", "main"],
  },
  output: {
    path: path.join(__dirname, "/public"),
    filename: "[name].js",
    chunkFilename: "[name].[id].js",
  },
  module: {
    rules: [
      {
        test: /\.svelte$/,
        use: {
          loader: "svelte-loader",
          options: {
            compilerOptions: {
              dev: !prod,
            },
            emitCss: prod,
            hotReload: !prod,
          },
        },
      },
      {
        test: /\.css$/,
        use: [
          MiniCssExtractPlugin.loader,
          {
            loader: "css-loader",
            options: {
              url: false,
            },
          },
        ],
      },
      // {
      //   test: /\.(png|gif|jpg)$/,
      //   type: "asset/resource",
      //   parser: {},
      //   generator: {
      //     filename: "./6/[hash].[ext]",
      //   },
      // },
      {
        // required to prevent errors from Svelte on Webpack 5+
        test: /node_modules\/svelte\/.*\.mjs$/,
        resolve: {
          fullySpecified: false,
        },
      },
    ],
  },
  mode,
  plugins: [
    new MiniCssExtractPlugin({
      filename: "[name].css",
    }),
    // new copyWebpackPlugin({
    //   patterns: [
    //     {
    //       from: __dirname + "/public/", //打包的静态资源目录地址
    //       to: "../dist/" + productName + "/", //打包到dist下面的public
    //     },
    //     {
    //       from: __dirname + "/public/build",
    //       to: "./" + productName + "/build",
    //     },
    //     {
    //       from: __dirname + "/public/css",
    //       to: "./" + productName + "/css",
    //     },
    //   ],
    // }),
  ],
  devtool: prod ? false : "source-map",
  devServer: {
    hot: true,
  },
};
